<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_Zend
 * @subpackage Cache
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * This is warapper for class Zend_Cache_Core and its children, because they don't implement something like caching disable.
 *
 * @package SmartL_Zend
 * @subpackage Cache
 */
class SmartL_Zend_Cache_Wrapper {

	/**
	 * Wrapped cache objetc.
	 */
	private $_cache;
	/**
	 * Determines whether the caching is disabled.
	 * @var boolean
	 */
	private $_disabled;
	
	/**
	 * Custructor.
	 *
	 * @param Zend_Cache_Core $cache The object that will be warapped.
	 */
	public function __construct(Zend_Cache_Core $cache, $disabled = false) {
		$this->_cache = $cache;
		$this->_disabled = $disabled;
	}
	
	/**
	 * Load method is warapped check for disabled cache is added.
	 * See parent's doc of it.
	 *
	 * @param string $id
	 * @param boolean $doNotTestCacheValidity
	 * @param boolean $doNotUnserialize
	 * @return unknown
	 */
	public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false) {
		if ($this->_disabled) {
			return false;
		} else {
			return $this->_cache->load ( $id, $doNotTestCacheValidity, $doNotUnserialize );
		}
	}
	
	/**
	 * Save method is wrapped check for disabled cache is added.
	 *
	 * @param midex $data
	 * @param string $id
	 * @param array $tags
	 * @param integer|boolean $specificLifetime
	 * @return boolean
	 */
	public function save($data, $id = null, array $tags = array(), $specificLifetime = false) {
		if (! $this->_disabled) {
			return $this->_cache->save ( $data, $id, $tags, $specificLifetime );
		} else {
			return true;
		}
	}
	
	public function getWrappedObject() {
		return $this->_cache;
	}
	
	public function isDisabled() {
		return $this->_disabled;
	}
	
	/**
	 * Calls all methods from the wrapped object.
	 */
	public function __call($method, array $options) {
		if (method_exists ( $this->_cache, $method )) {
			return call_user_func_array ( array ($this->_cache, $method ), $options );
		}
		require_once 'SmartL/Zend/Cache/Wrapper/Exception.php';
		throw new SmartL_Zend_Cache_Wrapper_Exception ( "Unknown method '" . $method . "' called!" );
	}
}